/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.druck;

import net.sf.paperclips.Margins;
import net.sf.paperclips.PaperClips;
import net.sf.paperclips.Print;
import net.sf.paperclips.PrintJob;
import net.sf.paperclips.StyledTextPrint;
import net.sf.paperclips.TextStyle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Shell;

import de.mnit.basis.swt.dialog.SWT_Fehler;
import de.mnit.basis.swt.element.steuer.rollbar.gruppe.bild.SWT_TextEditor;
import de.mnit.basis.sys.Ausgabe;

/**
 * @author Michael Nitsche
 * 17.06.2010	Erstellt
 */
public class SWT_DruckAuftrag {

	private final String titel;
	private boolean dialog=false;
	private final Shell shell;
	private Margins raender=null;


	public SWT_DruckAuftrag(String titel, Shell shell) {
		this.titel=titel;
		this.shell=shell;
	}


	public void sDruckDialog(boolean b) {
		this.dialog=b;
	}

	/**
	 * cm
	 */
	public void sRand(double l, double r, double o, double u) {
		this.raender=new Margins();
		l*=36; //Bei A4-Blattbreite 21,2 cm entspricht 2cm Rand 72
		r*=36;
		o*=36;
		u*=36;
		this.raender.left=(int)l;
		this.raender.right=(int)r;
		this.raender.top=(int)o;
		this.raender.bottom=(int)u;
	}

	public boolean drucken(Print daten) {
		PrinterData printerData=null;
		if(!dialog)
			printerData = Printer.getDefaultPrinterData();
		if(dialog || printerData==null) {
			PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
			printerData = dialog.open();
			if(printerData==null)
				return false; //Abgebrochen!
		}

		PrintJob job=new PrintJob(this.titel, daten);
		if(this.raender==null)
			job.setMargins(72);
		else
			job.setMargins(this.raender);
		try {
			PaperClips.print(job, printerData);
		}
		catch(Exception e) {
			SWT_Fehler.zeig(e, "Auftrag konnte nicht gedruckt werden!", false);
			return false;
		}
		return true;
	}

	public void druckenSchnipsel(SWT_TextEditor edit) {
		String text=edit.gText();
		StyleRange[] styles=edit.swt().getStyleRanges(true);

		TextStyle normal = new TextStyle().font("Courier", 10, SWT.NORMAL);
		TextStyle header = new TextStyle().font("Arial", 14, SWT.BOLD).underline();
		TextStyle umbruch= normal.fontHeight(6);
//		TextStyle bold = normal.fontStyle(SWT.BOLD);
//		TextStyle italic = normal.fontStyle(SWT.ITALIC);
//		TextStyle underline = normal.underline();
//		TextStyle strikeout = normal.strikeout();

		StyledTextPrint doc = new StyledTextPrint();

		int pos=0;
		for(StyleRange sr : styles) {
			if(sr.start>pos) {
				String teil=text.substring(pos, sr.start);
				iPlus(doc, teil, normal, umbruch);
				pos=sr.start;
			}
			TextStyle textStyle=normal;
			if(sr.font!=null)
				textStyle=header;
			else {
				if(sr.strikeout) textStyle=textStyle.strikeout();
				if(sr.underline) textStyle=textStyle.underline();
				textStyle=textStyle.fontStyle(sr.fontStyle);
			}
			String teil=text.substring(pos, pos+sr.length);
			int bis=pos+sr.length;
//			doc.append(teil, textStyle);
			iPlus(doc, teil, textStyle, umbruch);
			pos=bis;
		}
		if(pos<text.length()) {
			String teil=text.substring(pos, text.length());
			iPlus(doc, teil, normal, umbruch);
		}
		drucken(doc);
	}

	private boolean zuletztN=false;
	private void iPlus(StyledTextPrint doc, String text, TextStyle style, TextStyle umbruch) {
		Ausgabe.debug(text, style);
//		text=FormatText.zeilenumbruch(text);
		int pos=0;
		while(pos<text.length()) {
			if(text.charAt(pos)=='\n') {
				doc.setStyle(zuletztN ? style : umbruch); //Leerzeilen normale Zeile, sonst schmäler!
				doc.newline();
//				doc.append("\n");// geht nicht!
				pos++;
				zuletztN=true;
				continue;
			}
			zuletztN=false;
			int ziel=text.indexOf('\n', pos);
			if(ziel<0)
				ziel=text.length();
			String teil=text.substring(pos, ziel);
			for(int i=0; i<teil.length(); i++)
				doc.append(teil.charAt(i)+"", style); //Bricht sonst ggf. mit Fehler ab, wenn länger als Seitenbreite!
			pos+=teil.length();
		}
	}

}
